package cn.hg.jbsa.admin.provider;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;

import cn.hg.jboot.core.exception.BaseServiceException;
import cn.hg.jbsa.admin.model.JbsaDict;
import cn.hg.jbsa.admin.service.JbsaDictService;
import io.jboot.aop.annotation.Bean;
import io.jboot.service.JbootServiceBase;


@Bean
public class JbsaDictServiceProvider extends JbootServiceBase<JbsaDict> implements JbsaDictService {

    @Override
    public Page<JbsaDict> findPageByKv(Integer page, Integer limit, Kv para) throws BaseServiceException {
        /*
         * 通过SQL模板引擎构建SQL及参数
         */
        SqlPara sqlPara = Db.getSqlPara("system.findDict", para);

        /*
         * 执行查询
         */
        Page<JbsaDict> pageResult = JbsaDict.dao.paginate(page, limit, sqlPara);

        /**
         * 返回查询结果
         */
        return pageResult;
    }

    @Override
    public void updateDictChildredIds(JbsaDict dict) throws BaseServiceException {
        if (null == dict) {
            return;
        }

        String childrenIds =
            Db.queryFirst("SELECT GROUP_CONCAT(id) FROM jbsa_dict WHERE status<>'del' AND parent_id=?", dict.getId());
        dict.setChildrenIds(childrenIds);

        this.update(dict);
    }

}
